package com.vip8.recorder.service.web.controller;

import com.vip8.recorder.api.facade.DeviceFacade;
import com.vip8.recorder.api.model.base.MultiResult;
import com.vip8.recorder.api.model.base.SingleResult;
import com.vip8.recorder.api.model.dto.AppVersionDTO;
import com.vip8.recorder.api.model.dto.HeartbeatDTO;
import com.vip8.recorder.api.model.params.DeviceParams;
import com.vip8.recorder.api.model.params.VersionParam;
import com.vip8.recorder.service.db.mapper.HeartbeatDao;
import com.vip8.recorder.service.service.DeviceService;
import com.vip8.recorder.service.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/device")
@Validated
@Slf4j
public class DeviceController implements DeviceFacade {

    @Resource
    private DeviceService deviceService;


    @RequestMapping(value = "/heartbeat", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<Boolean>  heartbeat(@RequestBody String deviceParams) {
        DeviceParams params = JsonUtil.parseObject(deviceParams, DeviceParams.class);
        Boolean result = deviceService.heartbeat(params);
        return SingleResult.success(result);
    }

    @Override
    @RequestMapping(value = "/batchHeartbeat", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    public SingleResult<Boolean> batchHeartbeat(@RequestBody String batchParams) {
        List<DeviceParams> params = JsonUtil.parseList(batchParams, DeviceParams.class);
        Boolean result = deviceService.batchHeartbeat(params);
        return SingleResult.success(result);
    }

    @RequestMapping(value = "/checkVersion", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public SingleResult<AppVersionDTO> checkVersion(@RequestBody String versionParam) {
        VersionParam param  = JsonUtil.parseObject(versionParam, VersionParam.class);
        AppVersionDTO versionDTO =  deviceService.checkVersion(param);
        return SingleResult.success(versionDTO);
    }

    @RequestMapping(value = "/queryPersonDistribution", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<HeartbeatDTO> queryPersonDistribution(@RequestBody String deviceParams) {
        DeviceParams params = JsonUtil.parseObject(deviceParams, DeviceParams.class);
        return deviceService.queryPersonDistribution(params);
    }

    @RequestMapping(value = "/queryTrack", method = RequestMethod.POST)
    @ResponseBody
    @Validated
    @Override
    public MultiResult<HeartbeatDTO> queryTrack(@RequestBody String deviceParams) {
        DeviceParams params = JsonUtil.parseObject(deviceParams, DeviceParams.class);
        return deviceService.queryTrack(params);

    }
}
